package com.onlyphoto.onlymall.utils;


import java.text.DecimalFormat;

public class NumUtil {

    public static String getTwoDecimal(double num) {
        DecimalFormat dFormat = new DecimalFormat("#0.00");
        String yearString = dFormat.format(num);
        return yearString;
    }

    public static String getTwoDecimal(String num) {
        try {
            double n = Double.parseDouble(num);
            return getTwoDecimal(n);
        } catch (Exception e) {
            return "0.00";
        }

    }

    // 判断字符串对象为null或者""
    public static boolean isBlank(String str) {
        return (str == null || str.isEmpty() || "null".equals(str));
    }

    // 金额由10000分转为100元
    public static String toIntAccount(String num) {
        if (isBlank(num)) {
            return "0";
        }
        return toDivAccount2("" + Long.parseLong(num) / 100);
    }

    //金额由1000000分转为10,000元
    public static String toDivAccount(String num) {
        if (isBlank(num)) {
            return "0";
        }
        DecimalFormat df = new DecimalFormat("#,###");
        return df.format(Long.parseLong(num) / 100);
    }


    // 金额由10000.00转为10,000.00元
    public static String toDivAccount2(String num) {
        if (isBlank(num)) {
            return "0";
        }

        try{
            if(Double.parseDouble(num)==0.0){
                return "0.00";
            }

            DecimalFormat df = new DecimalFormat("#,###.00");
            return df.format(Double.parseDouble(num));
        }catch (Exception e){
            return "0.00";
        }

    }

    public static String getFormatMoney(String money) {
        if (money.contains(".00")) {
            return money.replace(".00", "");
        }else if(money.endsWith("0")){
            return money.subSequence(0,money.lastIndexOf("0")).toString();
        }
        else {
            return money;
        }
    }
}
